package primerdb;

/**
 *
 * @author Amjad_Almahairi
 */
import java.io.File;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class XMLExporter {
    
    Document doc;
    File docFile;
    Element rootElement;
    
    public XMLExporter(File docFile){
       this.docFile = docFile;
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            // root elements
            doc = docBuilder.newDocument();
            rootElement = doc.createElement("PrimersDB");
            doc.appendChild(rootElement);
            
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLExporter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void saveDoc() {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            
            StreamResult result = new StreamResult(docFile);
           
            transformer.transform(source, result);
            
            
//            // Output to console for testing
//            StreamResult result2 = new StreamResult(System.out);
//            transformer.transform(source, result2);
            
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(XMLExporter.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLExporter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Element exportFolder(Element parentFolder, Folder folder) {

        Element folderElem = doc.createElement("Folder");
        parentFolder.appendChild(folderElem);

        Attr attr = doc.createAttribute("id");
        attr.setValue(folder.getId().toString());
        folderElem.setAttributeNode(attr);

        Attr attr2 = doc.createAttribute("name");
        attr2.setValue(folder.getName());
        folderElem.setAttributeNode(attr2);

        return folderElem;
    }

    public Element exportPrimer(Element folderElem, Primer primer, boolean shortlisted) {

        Element primerElem = doc.createElement("Primer");
        folderElem.appendChild(primerElem);

        Element name = doc.createElement("Name");
        name.setTextContent(primer.getName());
        primerElem.appendChild(name);

        Element sequence = doc.createElement("Sequence");
        sequence.setTextContent(primer.getSequence());
        primerElem.appendChild(sequence);

        if (!shortlisted) {

            Element annTemp = doc.createElement("AnnTemp");
            annTemp.setTextContent(primer.getAnnTemp());
            primerElem.appendChild(annTemp);

            Element freezer = doc.createElement("Freezer");
            freezer.setTextContent(primer.getFreezer());
            primerElem.appendChild(freezer);

            Element target = doc.createElement("Target");
            target.setTextContent(primer.getTarget());
            primerElem.appendChild(target);

            Element conc = doc.createElement("Conc");
            conc.setTextContent(primer.getConc());
            primerElem.appendChild(conc);

            Element date = doc.createElement("Date");
            date.setTextContent(primer.getDate());
            primerElem.appendChild(date);

            Element label = doc.createElement("Label");
            label.setTextContent(primer.getLabel());
            primerElem.appendChild(label);


            Element notes = doc.createElement("Notes");
            notes.setTextContent(primer.getNotes());
            primerElem.appendChild(notes);

            Element project = doc.createElement("Project");
            project.setTextContent(primer.getProject());
            primerElem.appendChild(project);
        }
        return primerElem;
    }

    public Element exportPrimer(Element folderElem, Primer primer){
        
        return exportPrimer(folderElem, primer, false);
    }

    public static void main(String argv[]) {

        XMLExporter exporter = new XMLExporter(new File("primers.xml"));
        Folder folder = new Folder(4, "folder1");
        Element folderElem = exporter.exportFolder(exporter.rootElement, folder);
        Primer prim1 = new Primer("test", "", "", "", "", "", "", "", "", "",4);
        Primer prim2 = new Primer("test2", "", "", "", "", "", "", "", "", "",5);
        exporter.exportPrimer(folderElem, prim1);
        Folder folder2 = new Folder(5, "folder2");
        Element folderElem2 = exporter.exportFolder(folderElem, folder2);
        exporter.exportPrimer(folderElem2, prim2);
        exporter.saveDoc();
    }
}


    
   